﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using AMS.Core.Entities;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;

namespace AMS.Core.DataAccess
{
    public class CoreDataAccess:BaseDataAccess
    {
        public NameObjectDictionary GetAssetTypeInfos(StringCollection keyValues)
        {
            if (keyValues != null && keyValues.Count > 0)
            {
                string strHeadHql = "From "+typeof(AssetType).Name+" o where (";
                var nbd = new NameObjectDictionary();
                var sb = new StringBuilder(strHeadHql);
                bool notFirst = false;
                for (int i = 0; i < keyValues.Count; i++)
                {
                    string strKeyVal = keyValues[i];
                    if(strKeyVal.IndexOf(KEY_VAL_FG) == -1 || strKeyVal.EndsWith(KEY_VAL_FG))
                        continue;
                    string strTypeCode = strKeyVal.Substring(0, strKeyVal.IndexOf(KEY_VAL_FG));
                    string strTypeName = strKeyVal.Substring(strKeyVal.IndexOf(KEY_VAL_FG) + KEY_VAL_FG.Length);
                    if (notFirst)
                    {
                        sb.Append(" or ");
                    }
                    else
                    {
                        notFirst = true;
                    }
                    sb.Append("(o.TypeCode='"+strTypeCode+"' and o.Name='"+strTypeName+"')");

                    if (i % 100 == 0 || i == (keyValues.Count - 1))
                    {
                        sb.Append(") ");
                        IList list = Session.CreateQuery(sb.ToString()).List();

                        foreach (object obj in list)
                        {
                            if(obj is AssetType && obj !=null)
                            {
                                var objVal = obj as AssetType;
                                if(!string.IsNullOrEmpty(objVal.TypeCode) && !string.IsNullOrEmpty(objVal.Name))
                                {
                                    nbd[objVal.TypeCode + KEY_VAL_FG + objVal.Name] = objVal;
                                }
                            }
                        }
                        sb = new StringBuilder(strHeadHql);
                        notFirst = false;
                    }
                }


                return nbd;
            }
            else
            {
                return new NameObjectDictionary();
            }
        }

    }
}
